<template>
  <el-container class="home-comtainer">
    <!-- 头部区域 -->
    <el-header>
      <div class="left-header">
        <img src="@/assets/image/logo.png" width="3%" />
        <span>我的第一个后台管理系统</span>
      </div>
      <el-button type="info" @click="out">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <div class="shrink-button" @click="shrink">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          :default-active="isdefault"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="iscollapse"
          :collapse-transition="false"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menu" :key="item.id">
            <!-- 一级菜单的模版区域 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + items.path"
              v-for="items in item.children"
              :key="items.id"
              @click="isdefaultBut('/' + items.path)"
            >
              <i class="el-icon-menu"></i>
              <span>{{ items.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menu: [],
      iconObj: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      iscollapse: false,
      isdefault: ""
    };
  },
  methods: {
    // 点击退出按钮，清除sessionStorage里保存的toke，返回登录页面
    out() {
      window.sessionStorage.clear("");
      this.$router.push({ path: "/login" });
    },
    // 获取首页左侧菜单接口
    async HomeMenu() {
      const result = await this.$http.get("menus");
      this.menu = result.data;
    },
    //点击按钮，切换菜单的折叠与展开
    shrink() {
      this.iscollapse = !this.iscollapse;
    },
    isdefaultBut(val) {
      window.sessionStorage.setItem("default", val); // 将token存储到本地浏览器中
      this.isdefault = val;
    }
  },
  created() {
    this.HomeMenu();
    this.isdefault = window.sessionStorage.getItem("default");
  }
};
</script>

<style lang="stylus" scoped>
.home-comtainer
    height 100%
.el-header
    padding-left 0
    background #373d41;
    display flex
    align-items: center;
    justify-content: space-between
    .left-header
       display flex
       align-items center
       span
           color #ffffff
           font-size 24px
           margin-left 6px
.el-aside
    background #333744;
    .el-menu
        border none
    .shrink-button
        background #4a5064
        padding 6px 0
        color #ffffff
        text-align center
        letter-spacing 2px
        cursor pointer
.el-main
    background #eaedf1
.iconfont
    margin-right 6px
</style>
